import axios from "axios";
import { Message, MessageBox } from "element-ui";
import router from "../router/index";
// 创建axios实例
const service = axios.create({
  timeout: 60000, // 请求超时时间,
  method: "get",
  headers: {
    //'token':getToken("cmsOpsSid"),//业务接口统一在header中添加token
    "Content-Type": "application/json;charset=UTF-8",
  },
  params: {},
});

//request拦截器
service.interceptors.request.use(
  (config) => {
    // startLoading();
    if (localStorage.eleToken) {
      config.headers.Authorization = localStorage.eleToken;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// response 拦截器
service.interceptors.response.use(
  (response) => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    if (response.data.success == true) {
      return response.data;
    } else {
      return response.data;
    }
  },
  (error) => {
    // Message.error(error.response.data)
    // 获取错误的状态码
    if (error.response.status == 401) {
      Message.error("密码失效，请重新登录");
      localStorage.removeItem("eleToken");
      router.push("/login");
    }
    return Promise.reject(error);
  }
);

export default service;
